
Page({
  data: {
    itrahost:"https://itra.run",
    hadData:false,
    name:"",
    raceYearId:"",
    isLoaded:false,
    list:[],
    itraPoints:0,
    alldata:[]
  },

  onLoad:function(options){
    var obj =JSON.parse(decodeURIComponent(options.str))
    console.log(obj);
    this.setData({
        itraPoints:obj.itraPoints,
        raceYearId:obj.raceYearId
    })
  },

  onReady:function(){
    this.loadData()
  },

  loadData:function(){
      var that = this;
      const app = getApp();
      wx.request({
        url: app.globalData.raceDetail+this.data.raceYearId,
        method:"GET",
        success:(res)=>{
          var result = res.data;
          console.log(result);
          that.setData({
            isLoaded:true
          })
          if(result!=null && result.length>0){
            console.log(result.length);
            that.setData({
              hadData:true,
              list:result,
              alldata:result
            })
          }else{
            that.setData({
              hadData:false,
              noData:true
            })
          }
        },
        fail: (err) => {//请求失败
          this.setData({
            isLoaded:true,
            hadData:false
          })
        }
      })
  },

  onItem:function(e){
      var index = e.currentTarget.id;
      var obj = this.data.list[index];
      if(obj.firstName=='Anonymous' || obj.lastName == 'Anonymous')
        return;
      var info = {
        'firstName':obj.firstName,
        'lastName':obj.lastName,
        'runnerId':obj.runnerId
      };
      var str = encodeURIComponent(JSON.stringify(info));
      wx.navigateTo({
        url: '../mine/mine?str='+str
      })
  },
  onNameInputChange:function(options){
    let value = options.detail.value;
    this.setData({
      name: value
    })
    // console.log('===1==1===='+value);
    if(value==''){
      console.log('=====已清空====');
      this.setData({
        list: this.data.alldata,
        noData:this.data.alldata.length==0
      })
    }
  },
  fuzzyMatchIgnoreCase:function(str, pattern) {
    const regex = new RegExp(pattern.toLowerCase().split('').join('.*'), 'i');
    return regex.test(str.toLowerCase());
  },
  onSearch:function(){
    if(this.data.name=='') return;
    var sname = this.data.name.replace(/\s+/g, '').toLowerCase();
    var that = this;
    let resData = [];
    this.data.alldata.forEach(function(item, index) {
        var lname = item.lastName+item.firstName;
        var nname = lname.replace(/\s+/g, '').toLowerCase();
        var ret = that.fuzzyMatchIgnoreCase(nname,sname);
        if(ret){
          console.log(lname);
          resData.push(item);
        }
    });
    this.setData({
      list: resData,
      noData:resData.length==0
    })
  },
  
  onShareAppMessage:function(){
    var share = this.data.name;
    return {
        title: share+" 比赛成绩排行榜"
    };
  },
  onShareTimeline:function(){
    var share = this.data.name;
    return {
      title: share+" 比赛成绩排行榜"
    };
  },
})
